<template>
    <div>
        <el-row>
            <el-col>
                <el-form label-width="120px" :model="FinanceLoanDB">
                    <el-row>
                        <el-col :offset="2" :span="9">
                            <el-form-item label="借款人">
                                <el-select width="100%" v-model="FinanceLoanDB.Borrower" filterable placeholder="请选择申请人" style="width:100%">
                                    <el-option value="">请选择请款人</el-option>
                                    <el-option value=" ">全部</el-option>
                                    <el-option v-for="(v,k) in AllUsers" :value="v.Id" :data-userid="v.Id" :label="v.UserName" v-text="v.UserName" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :offset="2" :span="9">
                            <el-form-item label="金额">
                                <el-input v-model="FinanceLoanDB.Money"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="9" :offset="2">
                            <el-form-item label="借款时间" prop="BorrowTime">
                                <el-date-picker style="width: 100%"   v-model="FinanceLoanDB.BorrowTime"
                                                class="wmax"
                                                type="date"
                                                placeholder="选择日期">
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="9" :offset="2">
                            <el-form-item label="备注">
                                <el-input v-model="FinanceLoanDB.Remark"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <!-- <el-row>
                        <el-col :span="20" :offset="2">
                            <el-form-item label="图片上传">
                                <div class="imagediv">
                                    <ImagesAssembly :imglist="imgArray"></ImagesAssembly>
                                </div>

                                <upload-file ref="upload_file_refs" style="display: inline-block;margin-right: 10px;display: block"
                                             content="图片选择"
                                             list_type="picture-card"
                                             btnsize=""
                                             :another-data="AnotherData"
                                             :showfilelist="true"
                                            >

                                </upload-file>
                            </el-form-item>
                        </el-col>
                    </el-row> -->

                    <el-row :gutter="20" style="text-align: center">
                        <input type="hidden" id="Id" />
                        <!-- <el-button type="primary" @click="AddFinanceLoandetailsShow_event">添加明细</el-button> -->
                        <el-button type="success" @click="SaveFinanceLoan()">修改 </el-button>
                    </el-row>
                </el-form>
            </el-col>
        </el-row>
        <div>
            <el-dialog :visible.sync="AddFinanceLoandetailsShow" width="60%" title="新建单据明细" :append-to-body="true">
                <AddFinanceLoandetails></AddFinanceLoandetails>
            </el-dialog>

            <el-dialog :visible.sync="UpFinanceLoandetailsShow"  width="60%" title="修改单据明细" :append-to-body="true">
                <UpFinanceLoandetails :form="form"   ></UpFinanceLoandetails>
            </el-dialog>
        </div>
    </div>
</template>

<script>

    import * as publicjs from "/public/Scriput/Public"
    import AddFinanceLoandetails from "./FinanceLoandetails/AddFinanceLoandetails"
    import UpFinanceLoandetails from "./FinanceLoandetails/UpFinanceLoandetails"
    import UploadFile from "../../UploadFile";
    import ImagesAssembly from "../../ImagesAssembly";

    export default {
        name: "UpFinanceLoan",
        components: {
            'AddFinanceLoandetails': AddFinanceLoandetails,
            'UpFinanceLoandetails': UpFinanceLoandetails,
            "upload-file": UploadFile,
            'ImagesAssembly':ImagesAssembly,
        },
        props: {
            GetPayArray: {
                type: Array,
                required: true
            },
            AllUsers: {
                type: Array,
                required: true
            },
            AllProject: {
                type: Array,
                required: true
            },
            AllDepartment: {
                type: Array,
                required: true
            },
            FinanceLoanDB: {
                type: Object,
                required: true
            },
            editPayee: {
                type: Array,
                required: true
            },
            AllCategory: {
                type: Array,
                required: true
            },
        },
        watch: {
            editPayee: function (val, oval) {
                this.editPayeeArray = val
            },
            FinanceLoanDB: function (val) {
                this.GetdetailedDB();
                this.InitImg();
            },
        },
        created() {
            this.editPayeeArray = this.editPayee;
            this.GetdetailedDB();
            this.InitImg();
            this.FinanceTypeNumber(this.FinanceLoanDB.FinanceType);
        },
        data() {
            return {
                BaseUrl: this.$http.defaults.baseURL,
                editPayeeArray: [],
                objcss: {
                    display: "none",
                },
                AddFinanceLoandetailsShow: false,
                UpFinanceLoandetailsShow: false,
                src: '',
                bInt: 0,
                detailed: [
                ],
                form: {
                    Purpose: "",
                    Money: '',
                    CategoryTwo: '',
                    CategoryOne: '',
                    //HasInvoice: false,
                },
                imgArray: [],
                //文件上传参数
                AnotherData: {
                    FileType: 256,
                    id: 1,
                    Type: 0,
                    Single: false,
                },
                //手续费显示
                Two_ServiceCharge:false,
            }
        },
        methods: {
            async InitImg() {
                this.imgArray = await publicjs.GetAllFileByUrl(this.$http, "256," + this.FinanceLoanDB.Id);
            },
            GetdetailedDB() {
                if (this.FinanceLoanDB.TransferMethod == 2){
                    this.Two_ServiceCharge = true
                }else {
                    this.Two_ServiceCharge = false
                }
                if (this.FinanceLoanDB.LoanNumber != '') {
                    this.$http.get("/Finance/GetfinanceDocDetails?Number=" + this.FinanceLoanDB.LoanNumber +'&type=1').then(list => {
                        this.detailed = [];
                        if (list.data.length > 0) {
                            this.detailed = list.data;
                        }
                    });
                } else {
                    this.detailed = [];
                }
            },
            GetNameById(id) {
                for (var i = 0; i < this.AllCategory.length; i++) {
                    let Id = parseInt(id)
                    if (Id == this.AllCategory[i].Id) {
                        return this.AllCategory[i].CategoryName;
                    }
                }
            },

            AddFinanceLoandetailsShow_event() {
                this.bInt = 0;
                this.AddFinanceLoandetailsShow = true;
            },
            _GetAddAllMoney() {
                let sum = 0;
                for (var i = 0; i < this.detailed.length; i++) {
                    sum += parseFloat(this.detailed[i].Money);
                }
                this.FinanceLoanDB.Money = sum;
            },
            DelAddLoandetails(row, index) {
                this.detailed.splice(index, 1)
                this._GetAddAllMoney();
            },

            GetFinance(row) {
                this.form = row;
                this.UpFinanceLoandetailsShow = true;
            },
            SaveFinanceLoan() {
                let loadding = this.openLoading();
                //此处HasInvoice的取值方式是有bug，之前是从form中取值，导致前端出错，后面改成从FinanceLoanDB中取值后为了兼容app的接口对接，没有整合到FinanceLoanDB中
                this.$http.post("/Finance/UpdateFinanceLoan", { finance: this.FinanceLoanDB }).then(list => {
                    loadding.close();
                    if (list.data.IsSuccess) {
                        loadding.close();
                        this.$message.success("借款单修改成功");
                        this.$parent.$parent.UpFinanceLoanShow = false;
                        this.$parent.$parent._GetFinanceLoanDetail();
                    }
                    else {
                        this.$message.error("修改失败");
                    }
                });
            },
            //去掉空数组
            DeleteNullArr(data) {
                var arr = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].length != 0) {
                        if (data[i].CategoryTwo == "") {
                            data[i].CategoryTwo = 0;
                        }
                        arr.push(data[i]);
                    }
                }
                return arr;
            },
            FinanceTypeNumber(val) {
                if (val == 1) {
                    this.objcss.display = "none";
                }
                else {
                    this.objcss.display = "block";
                }
            },
        },
    }
</script>

<style scoped>
    .el-table th {
        padding: 0px;
    }
    .imagediv{
        padding: 0px 3px;
    }
    .el-select-dropdown__item {
        width: 270px !important;
    }
</style>
